<?php

require 'functions.php';
require_once 'data/data.php';
header("Content-type:text/html;charset=utf-8");

$email = input('email');
$password = input('password');
$pwd_again = input('pwd_again');
$username = input('username','Guest');
$age = input('age',18);
$gender = input('gender',1);
$courses = input('course',[]);
$courses = implode(',', $courses);
$birth = input('birth',1995-01-01);
$attr = input('attr',1);
$description = input('describ');

if (empty($email)) {
	error("邮箱不能为空");
	return;
}

if (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
	error("请输入正确的邮箱格式");
	return;
}

if (strlen($password)< 6 ) {
	error("密码必须大于6位");
	return;
}

if($pwd_again !== $password){
	error("密码不一致");
	return;
}

if (empty($username)) {
	error("请输入用户名");
	return;
}

//机密
$password=password_hash($password, PASSWORD_DEFAULT);

/*//建立到mysql服务器的链接
$conn = mysqli_connect(`locallhost:8088`,'root','123456');
//选定数据库
mysqli_select_db($conn,'demo');
//编辑$sql为一个php字符串，将相关变量拼接进去
$sql = "insert into user(email,user_name,password,gender,courses,birthday,attr,introduction) values('{$email}','{$username}','{$password}',{$gender},'{$courses}','{$birth}','{$attr}','{$description}')";
//执行sql语句
mysqli_query($conn,$sql);
//获取受影响的行数
$affected = mysqli_affected_rows($conn);*/

$sql = "insert into user(email,user_name,password,gender,courses,birthday,attr,introduction) values(?,?,?,?,?,?,?,?)";
$args = [$email,$username,$password,$gender,$courses,$birth,$attr,$description];
$affected = execute($sql,$args);

//根据影响的行数判断sql语句是否执行成功
if ($affected === 1) {
	success('新增用户信息成功');
}else{
	error('新增用户信息失败');
	return;
}




/*function error($info,$url = 'form.php')
{
	echo "<h3>$info</h3>";
	header('Refresh:3; url=' . $url);
}

function input($name,$default = '')
{
	$value = $default;
	if (isset($_POST[$name])) {
		if (gettype($_POST[$name]) == 'string') {
			$value = htmlspecialchars($_POST[$name]);
		}else{
			$value = $_POST[$name];
		}
	}
	return $value;
}*/
?>


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>message</title>
</head>
<body>
  <ul>
  	<li>邮箱：<?=$email ?></li>
  	<li>密码：<?=$password ?></li>
  	<li>确认密码：<?=password_hash($pwd_again, PASSWORD_DEFAULT) ?></li>
  	<li>用户名：<?=$username ?></li>
  	<li>年龄：<?=$age ?></li>
  	<li>性别：<?=$gender == 1?'男':'女' ?></li>
  	<li>主修：<?=$courses ?></li>
  	<li>生日：<?=$birth ?></li>
    <li>属性：<?=$attr ?></li>
    <li>自我介绍：<?=$description ?></li>
  </ul>
</body>
</html>